﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using GTasks;
using GTasksPlus.Code;
using Microsoft.Phone.Controls;

namespace GTasksPlus
{
    public partial class MainPage
    {
        private string _currentId = String.Empty;
        
        public MainPage()
        {
            InitializeComponent();
        }

        #region Event Handlers

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var currentItem = sender as MenuItem;
            if (currentItem != null)
                NavigationService.Navigate(new Uri("/Views/ManipulateList.xaml?Id=" + currentItem.Tag,UriKind.RelativeOrAbsolute));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var currentItem = sender as Button;
            if (currentItem != null)
            {
                _currentId = currentItem.Tag.ToString();
                NavigationService.Navigate(new Uri("/Views/TaskView.xaml?Id=" + _currentId, UriKind.RelativeOrAbsolute));
            }
        }

        private void Add_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/ManipulateList.xaml?Id=new", UriKind.RelativeOrAbsolute));
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            App.MainViewModel.LoadData();
        }

        private void Logout_Click(object sender, EventArgs e)
        {
            Logout();
            while (NavigationService.CanGoBack)
                NavigationService.RemoveBackEntry();
            NavigationService.Navigate(new Uri("/Login.xaml", UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ApplicationTitle.Text = GTasksSettings.ApplicationTitle;
            if (IsLoggedIn())
                DataContext = App.MainViewModel;
            else
                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.RelativeOrAbsolute));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            while(NavigationService.CanGoBack)
                NavigationService.RemoveBackEntry();
        }

        private void CheckBox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var checkBox = sender as CheckBox;
            if (checkBox == null)
                return;

            //var refreshButton = ApplicationBar.Buttons[1] as ApplicationBarIconButton;
            //if (refreshButton != null)
            //    refreshButton.IsEnabled = (sender as CheckBox).IsEnabled;
        }

        #endregion

        private void About_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}